<template>
  <div>
    <el-card class="box-card" style="margin: 10px 0">
      <!-- 头部区域 -->
      <div slot="header" class="clearfix">
        <!-- 头部左侧内容 -->
        <el-tabs v-model="activeName" class="tab">
          <el-tab-pane label="销售额" name="sale" />
          <el-tab-pane label="访问量" name="visit" />
        </el-tabs>
        <!-- 头部右侧内容 -->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            class="date"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
          />
        </div>
      </div>
      <!-- 内容区域 -->
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <!-- 容器 -->
            <div ref="charts" class="charts" />
          </el-col>
          <el-col :span="6">
            <div class="rank">门店{{ title }}排名</div>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span class="brand">肯德基</span>
                <span class="rvalue">323,234</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span class="brand">麦当劳</span>
                <span class="rvalue">299,132</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span class="brand">必胜客</span>
                <span class="rvalue">278,998</span>
              </li>
              <li>
                <span class="rindex">4</span>
                <span class="brand">海底捞</span>
                <span class="rvalue">266,223</span>
              </li>
              <li>
                <span class="rindex">5</span>
                <span class="brand">西贝莜面村</span>
                <span class="rvalue">223,554</span>
              </li>
              <li>
                <span class="rindex">6</span>
                <span class="brand">汉堡王</span>
                <span class="rvalue">216,776</span>
              </li>
              <li>
                <span class="rindex">7</span>
                <span class="brand">真功夫</span>
                <span class="rvalue">197,353</span>
              </li>
              <li>
                <span class="rindex">8</span>
                <span class="brand">乡村基</span>
                <span class="rvalue">178,562</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'
export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      mycharts: null,
      // 收集日历数据
      date: []
    }
  },
  computed: {
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    }
  },
  watch: {
    title() {
      // 重新修改图标的配置信息
      this.mycharts.setOption({
        title: {
          text: this.title + '趋势'
        }
      })
    }
  },
  mounted() {
    this.mycharts = echarts.init(this.$refs.charts)
    this.mycharts.setOption({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '0',
        bottom: '0',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: [
            '一月',
            '二月',
            '三月',
            '四月',
            '五月',
            '六月',
            '七月',
            '八月',
            '九月',
            '十月',
            '十一月',
            '十二月'
          ],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [10, 52, 200, 334, 390, 330, 220, 110, 98, 167, 123, 44],
          color: '#ffa07a'
        }
      ]
    })
  },
  methods: {
    // 今日
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style lang="scss" scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
::v-deep .el-card__header {
  padding-bottom: 0;
  border-bottom: none;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
  span {
    margin: 0 10px;
    font-size: 14px;
  }
}

.date {
  width: 220px;
  margin: 0 20px;
}

.charts {
  width: 100%;
  height: 300px;
}

.rank {
  font-weight: 800;
}

ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
  li {
    height: 8%;
    margin: 10px 0;
    .rindex {
      float: left;
      width: 23px;
      height: 23px;
      border-radius: 50%;
      background-color: black;
      color: white;
      text-align: center;
    }
    .brand {
      margin-left: 50px;
    }
    .rvalue {
      float: right;
    }
  }
}
</style>
